#ifndef REMOTE_SERVER_SERVER_SERVER_H_
#define REMOTE_SERVER_SERVER_SERVER_H_

#include "vzconn/conn/vzserver.h"
#include "boost/asio/steady_timer.hpp"
#include "remote/control/controlsession.h"
#include "remote/base/basedefines.h"
#include <map>

namespace remote {

class RemoteServer : public boost::noncopyable,
  public boost::enable_shared_from_this < RemoteServer > {
 public:
  typedef boost::shared_ptr<RemoteServer> Ptr;
  RemoteServer(boost::asio::io_service &io_service);
  virtual ~RemoteServer();
  // default addr 127.0.0.1
  // default port 5299
  bool StartRemoteServer(RemoteServerSettings &settings);
  void StopRemoteServer();
 private:
  void OnNewControlConnect(vzconn::VzServer::Ptr tcp_server,
                           vzconn::VzConnect::Ptr tcp_connect);
  void OnControlServerError(vzconn::VzServer::Ptr tcp_server,
                            const boost::system::error_code& err);

  void OnSessionRegister(ControlSession::Ptr control_session, bool *res);
  void OnSessionClose(ControlSession::Ptr control_session);
  void OnSessionEvent(ControlSession::Ptr control_session,
                      QueryStanza::Ptr stanza);

  void ProcessServerEvent(ControlSession::Ptr control_session,
                          QueryStanza::Ptr stanza);
  void ProcessGetOnlineSession(ControlSession::Ptr control_session,
                               QueryStanza::Ptr stanza);
 private:
  boost::asio::io_service &io_service_;
  vzconn::VzServer::Ptr tcp_server_;
  typedef std::map<std::string, ControlSession::Ptr> ControlSessions;
  ControlSessions control_sessions_;
};
}

#endif // REMOTE_SERVER_SERVER_SERVER_H_